home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / doc / tree_example.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  156 lines

  1. ;;$File Tree_Example.pro
  2. ;;
  3. ;; DESCRIPTION:
  4. ;;
  5. ;;    This file contains example routines that call the TREE_XXXX routines
  6. ;;    contained in the file idl_tree.pro. To run this program, enter the
  7. ;;    following commands at the IDL prompt:
  8. ;;
  9. ;;    IDL> .run idl_tree
  10. ;;    IDL> tree_example
  11. ;;
  12. ;;    The TREE_NEW, TREE_TRAVERSE, and TREE_DELETENODE routines are
  13. ;;    found in the file idl_tree.pro.
  14. ;;
  15. ;;
  16. ;; MODIFICATION HISTORY:
  17. ;;    March 1994,  -KDB    Initial coding
  18. ;;    August 1996,  -KDB    Updated to use pointers
  19. ;;
  20. ;;============================================================================
  21.  
  22. PRO CmpData, d1, d2, result
  23. ;;
  24. ;; Do the comparison of the data
  25.  
  26.   result= (d1.data lt d2.data)*(-1) + (d1.data eq d2.data)*0  $
  27.             + (d1.data gt d2.data)*1
  28. END
  29. ;;============================================================================
  30.  
  31. PRO CmpTime, d1, d2, result
  32. ;;
  33. ;; Do the comparison of the time data
  34.   result= (d1.time lt d2.time)*(-1) + (d1.time eq d2.time)*0  $
  35.                         + (d1.time gt d2.time)*1
  36. END
  37.  
  38. ;;============================================================================
  39.  
  40. PRO PrintStruct, Data
  41. ;;
  42. ;; Print the data
  43.  
  44.    print,"Time: ", Data.time, " Data: ", Data.data
  45.  
  46. END
  47.  
  48. ;;===========================================================================
  49.  
  50. PRO Tree_Example
  51.  
  52. ;; Make a structure to hold our fake data. With pointers, this can
  53. ;; be any IDL type.
  54.  
  55.    Cnt = 10
  56.  
  57.    DATATYPE = {time:0d0, data:0.0}
  58.  
  59.    MyData = replicate(DATATYPE, Cnt)
  60.  
  61. ;; Create some data
  62.  
  63.    MyData.data = randomn(Seed, Cnt)
  64.  
  65. ;; Put some time tags on the data
  66.  
  67.    MyData.time = Systime(1)*randomu(Seed, Cnt)
  68.  
  69. ;; Now lets make a tree that will store the data in time order and one
  70. ;; that holds the data in data order.  "Tree_new" is found in the file
  71. ;; idl_tree.pro:
  72.  
  73.    Tree_new, DataTree, 'CmpData'
  74.    Tree_new, TimeTree, 'CmpTime'
  75.  
  76. ;; Go through and place the data into the trees:
  77.  
  78.    for i = 0, Cnt-1 do begin
  79.       Tree_Insert, TimeTree, MyData(i)
  80.       Tree_Insert, DataTree, MyData(i)
  81.    endfor
  82.  
  83. ;; Print out the trees
  84.  
  85.    Print, "Here is the tree sorted by TIME values:"
  86.    Tree_Traverse, TimeTree,  'PrintStruct', /INORDER
  87.  
  88.    Print, "Here is the tree sorted by DATA values:", Format='(/,A)'
  89.    Tree_Traverse, DataTree, 'PrintStruct', /INORDER
  90.  
  91. ;;
  92.    Print, "Press any key to continue...", Format='(/,A,$)'
  93.    a=Get_Kbrd(1)
  94. ;
  95. ;; Lets search for some data
  96. ;
  97.    Print, ""
  98. ;
  99. ;;Search for the fourth data value:
  100. ;
  101.    Print, "Searching for the fourth data value:", MyData(3)
  102. ;
  103.    pData = Tree_Search(TimeTree, MyData(3))
  104. ;
  105.    if(not PTR_VALID(pData))then     $
  106.     print,"Data Not Found"                $
  107.    else BEGIN
  108.     print, "Data Found, Value :", *pData
  109.    ENDelse
  110. ;
  111. ;; Now delete some nodes in the tree
  112. ;
  113.    Print,""
  114.    Print, "Deleting the node that contains:", MyData(3)
  115.  
  116.    Tree_DeleteNode, DataTree, MyData(3)
  117.    Tree_DeleteNode, TimeTree, MyData(3)
  118. ;
  119. ;; Now do another search for the node we just deleted
  120. ;
  121.    Print, ""
  122. ;
  123.    Print, "Now performing a search for the node we just deleted..."
  124.    Print, "Searching for :", MyData(3)
  125. ;
  126.    pData = Tree_Search(TimeTree, MyData(3))
  127. ;
  128.    if(not PTR_VALID(pData(0)))then   $
  129.         print,"Data Not Found"                          $
  130.    else BEGIN
  131.         print, "Data Found, Value :", *pData
  132.    ENDelse
  133. ;
  134.    Print,""
  135. ;
  136.    Print, "Now we also delete the node containing the second data value."
  137.    Print, "Deleting node :", MyData(1)
  138. ;
  139.    Tree_DeleteNode, DataTree, MyData(1)
  140.    Tree_DeleteNode, TimeTree, MyData(1)
  141. ;
  142.    Print, ""
  143.    Print, "Finally, we print the resulting trees..."
  144.    Print, "Sorted by TIME values:", Format='(/,A)'
  145.    Tree_Traverse, TimeTree, 'printStruct', /INORDER
  146. ;
  147.    Print, "Sorted by DATA values:", Format='(/,A)'
  148.    Tree_Traverse, DataTree, 'printStruct', /INORDER
  149. ;
  150. ;; Now delete the trees
  151. ;
  152.    Tree_Delete, DataTree
  153.    Tree_Delete, TimeTree
  154. ;
  155. END
  156.